OOUIHTMLForm: Support setWrapperLegend()
authorBartosz Dziewoński <matma.rex@gmail.com>
Sat, 11 Jul 2015 16:46:05 +0000 (18:46 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Mon, 13 Jul 2015 17:20:08 +0000 (19:20 +0200)
Changed FormSpecialPage not to call setWrapperLegend() for OOUI
forms to preserve current default behavior.

Bonus:
* Correct documentation of setWrapperLegend() to state that it
  HTML-escapes legend text.
* Remove hard-coded class="visualClear" in getFormAttributes().
* Allow setWrapperLegend( true ) to display the wrapper without
  legend text.
* Rejigger things so that we can put the legend and "header HTML"
  into correct order.

Bug: T103026
Change-Id: I847c5e18ae5469aa3a68cc9fa37b2a6614476ca2

includes/htmlform/HTMLForm.php
includes/htmlform/OOUIHTMLForm.php
includes/htmlform/VFormHTMLForm.php
includes/specialpage/FormSpecialPage.php
resources/src/mediawiki/mediawiki.htmlform.ooui.css

index 702651e..43cac88 100644 (file)
@@ -869,7 +869,8 @@ class HTMLForm extends ContextSource {
 
                $html = ''
                        . $this->getErrors( $submitResult )
-                       . $this->mHeader
+                       // In OOUI forms, we handle mHeader elsewhere. FIXME This is horrible.
+                       . ( $this->getDisplayFormat() === 'ooui' ? '' : $this->mHeader )
                        . $this->getBody()
                        . $this->getHiddenFields()
                        . $this->getButtons()
@@ -893,7 +894,6 @@ class HTMLForm extends ContextSource {
                $attribs = array(
                        'action' => $this->getAction(),
                        'method' => $this->getMethod(),
-                       'class' => array( 'visualClear' ),
                        'enctype' => $encType,
                );
                if ( !empty( $this->mId ) ) {
@@ -912,10 +912,11 @@ class HTMLForm extends ContextSource {
        function wrapForm( $html ) {
                # Include a <fieldset> wrapper for style, if requested.
                if ( $this->mWrapperLegend !== false ) {
-                       $html = Xml::fieldset( $this->mWrapperLegend, $html );
+                       $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false;
+                       $html = Xml::fieldset( $legend, $html );
                }
 
-               return Html::rawElement( 'form', $this->getFormAttributes(), $html );
+               return Html::rawElement( 'form', $this->getFormAttributes() + array( 'class' => 'visualClear' ), $html );
        }
 
        /**
@@ -1219,9 +1220,10 @@ class HTMLForm extends ContextSource {
         * Prompt the whole form to be wrapped in a "<fieldset>", with
         * this text as its "<legend>" element.
         *
-        * @param string|bool $legend HTML to go inside the "<legend>" element, or
-        * false for no <legend>
-        *     Will be escaped
+        * @param string|bool $legend If false, no wrapper or legend will be displayed.
+        *     If true, a wrapper will be displayed, but no legend.
+        *     If a string, a wrapper will be displayed with that string as a legend.
+        *     The string will be escaped before being output (this doesn't support HTML).
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -1409,19 +1411,26 @@ class HTMLForm extends ContextSource {
                                if ( $sectionName ) {
                                        $config['id'] = Sanitizer::escapeId( $sectionName );
                                }
+                               if ( is_string( $this->mWrapperLegend ) ) {
+                                       $config['label'] = $this->mWrapperLegend;
+                               }
                                $fieldset = new OOUI\FieldsetLayout( $config );
                                // Ewww. We should pass this as $config['items'], but there might be string snippets.
                                $fieldset->group->appendContent( new OOUI\HtmlSnippet( $html ) );
-                               $html = $fieldset->toString();
+                               $html = $fieldset;
                        } else {
                                $html = Html::rawElement( 'div', $attribs, "\n$html\n" );
                        }
                }
 
-               if ( $this->mSubSectionBeforeFields ) {
-                       return $subsectionHtml . "\n" . $html;
+               if ( $subsectionHtml ) {
+                       if ( $this->mSubSectionBeforeFields ) {
+                               return $subsectionHtml . "\n" . $html;
+                       } else {
+                               return $html . "\n" . $subsectionHtml;
+                       }
                } else {
-                       return $html . "\n" . $subsectionHtml;
+                       return $html;
                }
        }
 
index 80e91f7..fe2f26e 100644 (file)
  * Compact stacked vertical format for forms, implemented using OOUI widgets.
  */
 class OOUIHTMLForm extends HTMLForm {
-       /**
-        * Wrapper and its legend are never generated in OOUI mode.
-        * @var boolean
-        */
-       protected $mWrapperLegend = false;
-
        public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
                parent::__construct( $descriptor, $context, $messagePrefix );
                $this->getOutput()->enableOOUI();
@@ -110,23 +104,30 @@ class OOUIHTMLForm extends HTMLForm {
                return $html;
        }
 
-       function getFormAttributes() {
-               $attribs = parent::getFormAttributes();
-               if ( !isset( $attribs['class'] ) ) {
-                       $attribs['class'] = '';
+       function getBody() {
+               $fieldset = parent::getBody();
+               // FIXME This only works for forms with no subsections
+               if ( $fieldset instanceof OOUI\FieldsetLayout ) {
+                       $fieldset->group->prependContent( new OOUI\HtmlSnippet( $this->mHeader ) );
                }
-
-               if ( is_string( $attribs['class'] ) ) {
-                       $attribs['class'] = trim( $attribs['class'] . ' mw-htmlform-ooui' );
-               } else {
-                       $attribs['class'][] = 'mw-htmlform-ooui';
-               }
-
-               return $attribs;
+               return $fieldset;
        }
 
        function wrapForm( $html ) {
-               // Always discard $this->mWrapperLegend
-               return Html::rawElement( 'form', $this->getFormAttributes(), $html );
+               $form = new OOUI\FormLayout( $this->getFormAttributes() + array(
+                       'classes' => array( 'mw-htmlform-ooui' ),
+                       'content' => new OOUI\HtmlSnippet( $html ),
+               ) );
+
+               // Include a wrapper for style, if requested.
+               $form = new OOUI\PanelLayout( array(
+                       'classes' => array( 'mw-htmlform-ooui-wrapper' ),
+                       'expanded' => false,
+                       'padded' => $this->mWrapperLegend !== false,
+                       'framed' => $this->mWrapperLegend !== false,
+                       'content' => $form,
+               ) );
+
+               return $form;
        }
 }
index f1fd05d..c544f16 100644 (file)
@@ -65,7 +65,7 @@ class VFormHTMLForm extends HTMLForm {
 
        protected function getFormAttributes() {
                $attribs = parent::getFormAttributes();
-               array_push( $attribs['class'], 'mw-ui-vform', 'mw-ui-container' );
+               $attribs['class'] = array( 'mw-ui-vform', 'mw-ui-container', 'visualClear' );
                return $attribs;
        }
 
index 9056761..42c5980 100644 (file)
@@ -96,7 +96,11 @@ abstract class FormSpecialPage extends SpecialPage {
                        $this->getMessagePrefix()
                );
                $form->setSubmitCallback( array( $this, 'onSubmit' ) );
-               $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
+               if ( $this->getDisplayFormat() !== 'ooui' ) {
+                       // No legend and wrapper by default in OOUI forms, but can be set manually
+                       // from alterForm()
+                       $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
+               }
 
                $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
                if ( !$headerMsg->isDisabled() ) {
index 92294c9..31d9854 100644 (file)
@@ -1,7 +1,8 @@
 /* OOUIHTMLForm styles */
 
-.mw-htmlform-ooui {
+.mw-htmlform-ooui-wrapper {
        width: 50em;
+       margin: 1em 0;
 }
 
 .mw-htmlform-ooui .mw-htmlform-submit-buttons {